package org.fast.develop.logicflow.runtime.convert.builder.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import lombok.Data;
import org.fast.develop.logicflow.builder.AbstractLogicFlowNodeBuilder;
import org.fast.develop.logicflow.core.meta.base.Resource;
import org.fast.develop.logicflow.core.meta.exception.LogicFlowMetaException;
import org.fast.develop.logicflow.core.meta.shape.task.JavaClassTask;
import org.fast.develop.logicflow.runtime.convert.builder.DesignerModelBuilder;
import org.fast.develop.logicflow.runtime.convert.designer.model.DesignerNodeModel;

import java.util.List;
import java.util.Objects;

/**
 * @author czh
 * @version V1.0
 * @data Created on 2022-12-31 10:18
 */
@Data
public class DesignerJavaClassTaskModelBuilder implements DesignerModelBuilder<DesignerNodeModel> {

    private final String DEFAULT_NAME = "自定义Java类";

    private DesignerNodeModel designerModel;

    private AbstractLogicFlowNodeBuilder logicFlowBuilder;

    @Override
    public AbstractLogicFlowNodeBuilder convert() {
        if (Objects.nonNull(this.getDesignerModel()) && Objects.nonNull(this.getLogicFlowBuilder())) {
            String name = DEFAULT_NAME;
            String className = null;
            if (Objects.nonNull(designerModel.getText()) && designerModel.getText().containsKey("value")) {
                name = (String) designerModel.getText().get("value");
            }
            if (Objects.nonNull(designerModel.getProperties())) {
                if (designerModel.getProperties().containsKey("className")) {
                    className = (String) designerModel.getProperties().get("className");
                } /*else {
                    throw new LogicFlowException("javaClassTask未配置类名，解析失败");
                }*/
            }
//            try {
                return logicFlowBuilder
                        .javaClassTask(designerModel.getId())
                        .name(name)
                        .className(className)
//                        .taskClass(Class.forName(className))
                        ;
//            } catch (ClassNotFoundException e) {
//                throw new LogicFlowException(StrUtil.format("解析错误，节点名称为:{} 类名 = {} 不存在", name, className));
//            }
        } else {
            throw new LogicFlowMetaException("节点信息缺失");
        }
    }

    @Override
    public String getType() {
        return "javaClassTask";
    }

    @Override
    public void convert(List<JSONObject> jsonObjects, Resource resource) {
        JavaClassTask javaClassTask = (JavaClassTask) resource;
        JSONObject startObj = new JSONObject();
        startObj.set("type", getType());
        startObj.set("resourceId", javaClassTask.getResourceId());
        JSONArray incoming = new JSONArray();
        JSONArray outgoing = new JSONArray();
        javaClassTask.getIncoming().forEach(item -> {
            incoming.add(new JSONObject().set("resourceId", item.getResourceId()));
        });
        javaClassTask.getOutgoing().forEach(item -> {
            outgoing.add(new JSONObject().set("resourceId", item.getResourceId()));
        });
        startObj.set("incoming", incoming);
        startObj.set("outgoing", outgoing);
        JSONObject properties = new JSONObject();
        properties.set("className", javaClassTask.getProperties().getClassName());
        startObj.set("properties", properties);
        jsonObjects.add(startObj);
    }
}
